/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


/**
 *
 * @author Johan
 */


public class UserPanel extends JPanel implements ActionListener{
    private JTextField amountField;
    private JTextField recieverField;
    private JButton activeButton;
    private JRadioButton withdrawButton;
    private JRadioButton depositButton;
    private JRadioButton transferButton;
    private ButtonGroup buttonGroup;
    private JButton logoutButton;
    private JLabel resources;
    private JComboBox accountList;
    private String[] accounts;
    String currentCivicReg;
    
    MainPanel mainPanel;
    GridBagConstraints c;
    public UserPanel(MainPanel mainPanel){
        this.mainPanel = mainPanel;
        setLayout(new GridBagLayout());
        c = new GridBagConstraints();
        c.weightx = 0.5;
       
        c.weighty = 0.5;
        c.gridheight = 1;
        c.gridwidth = 1;
        createComponents();
        addComponents();
    }
    private void createComponents(){
        resources = new JLabel();
        amountField = new JTextField(8);
        recieverField = new JTextField(8);
        recieverField.setText("Reciever...");
        recieverField.setEnabled(false);
        
        activeButton = new JButton("do stuff");
        activeButton.addActionListener(this);
        amountField.setText("Amount...");
        logoutButton = new JButton("logout!");
        logoutButton.addActionListener(this);
        
        withdrawButton = new JRadioButton("Withdraw");
        withdrawButton.addActionListener(this);
        depositButton = new JRadioButton("Deposit");
        depositButton.addActionListener(this);
        transferButton = new JRadioButton("Transfer");
        transferButton.addActionListener(this);
        buttonGroup = new ButtonGroup();
        resources.setText("lol");
       // getAccounts();
        accountList = new JComboBox();
        accountList.addActionListener(this);
        
    }
    public void setCurrentCivicReg(String civicReg){
        currentCivicReg = civicReg;
    }
    public void getAccounts(){
        accountList.removeAllItems();
        accounts = mainPanel.dbHandler.getAccounts(currentCivicReg);
        
        for(int i = 0; i < accounts.length; i++){
            accountList.addItem(accounts[i]);
        }   
    }
    private void changeLabel(){
        String a = ""+ mainPanel.dbHandler.getBalance(accountList.getSelectedItem().toString());
        resources.setText(a);
    }
    private void addComponents(){
        buttonGroup.add(withdrawButton);
        buttonGroup.add(depositButton);
        buttonGroup.add(transferButton);
        c.gridx = 0;
        c.gridy = 0;
        
        add(accountList,c);
        c.gridx = 1;
        add(resources,c);
        //c.gridx = 0;
        c.gridy = 1;
        add(withdrawButton,c);
        c.gridx = 1;
        add(depositButton,c);
        c.gridx = 2;
        add(transferButton,c);
        c.gridy = 2;
        c.gridx = 0;
        add(amountField,c);
        c.gridx = 1;
        add(recieverField,c);
        c.gridx = 2;
        add(activeButton,c);
        c.gridy = 3;
        c.gridx = 1;
        add(logoutButton,c);
        
        
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == logoutButton){
            mainPanel.swapScreen(MainPanel.LOGINPANEL);
        }else if(e.getSource() == activeButton){
            changeLabel();
            if(transferButton.isSelected()){
                int amount = Integer.parseInt(amountField.getText());
                String currentAccount = accountList.getSelectedItem().toString();
                String recieverAccount = recieverField.getText();
                if(mainPanel.dbHandler.getBalance(currentAccount)>=amount){
                    mainPanel.dbHandler.setBalance(currentAccount,-amount);
                    mainPanel.dbHandler.setBalance(recieverAccount,amount);
                }
            }else if(withdrawButton.isSelected()){
                int amount = Integer.parseInt(amountField.getText());
                String currentAccount = accountList.getSelectedItem().toString();
                if(mainPanel.dbHandler.getBalance(currentAccount)>=amount){
                    mainPanel.dbHandler.setBalance(currentAccount,-amount);
                }
                
            }else if(depositButton.isSelected()){
                int amount = Integer.parseInt(amountField.getText());
                String currentAccount = accountList.getSelectedItem().toString();
               
                mainPanel.dbHandler.setBalance(currentAccount,amount);
                
                
            }
        }else if(e.getSource() == transferButton){
            recieverField.setEnabled(true);
        }else if(e.getSource() == withdrawButton){
            recieverField.setEnabled(false);
        }else if(e.getSource() == depositButton){
            recieverField.setEnabled(false);
        }else if(e.getSource() == accountList){
            changeLabel();
        }
    }

    
}
